package com.ctg.payroll2.service.payment.impl;

import java.sql.SQLException;
import java.util.List;

import com.ctg.payroll2.bean.filter.payment.PayClazzItemFilter;
import com.ctg.payroll2.bean.filter.payment.PayDataFilter;
import com.ctg.payroll2.bean.po.payment.PayClassitem;
import com.ctg.payroll2.bean.po.payment.PayData;
import com.ctg.payroll2.bean.po.payment.PayEmpdata;
import com.ctg.payroll2.bean.po.payment.PayInput;
import com.ctg.payroll2.dao.payment.PayClazzItemDao;
import com.ctg.payroll2.dao.payment.PayEmpdataDao;
import com.ctg.payroll2.service.payment.PayEmpdataService;
import com.ctg.payroll2.util.CalDate;
import com.ctg.platform.util.Page;

public class PayEmpdataServiceImpl implements PayEmpdataService {
	private PayEmpdataDao dao;
	private PayClazzItemDao clazzItemDao;

	/**
	 * @param dao 设置dao
	 */
	public void setDao(PayEmpdataDao dao) {
		this.dao = dao;
	}

	public void setClazzItemDao(PayClazzItemDao clazzItemDao) {
		this.clazzItemDao = clazzItemDao;
	}

	public List<PayData> queryPayDataList(PayDataFilter filter) throws Exception {
		return dao.queryPayDataList(filter);
	}

	public Page queryPayTotal(PayDataFilter filter) {
		return dao.queryPayTotal(filter);
	}
	
	public List<PayEmpdata> queryPayClassEmpList(PayDataFilter filter) throws SQLException {
		return dao.queryPayClassEmpList(filter);
	}

	public void deletePayEmpDataBatch(String ids) {
		dao.deletePayEmpDataBatch(ids);
	}

	public void insertPayEmpdata(List<PayEmpdata> list) {
		dao.insertPayEmpdata(list);		
	}
	
	public PayInput queryPayInput(PayInput filter) throws Exception{
		PayInput payInput = null;
		
		PayClassitem itemThisMonth = queryPayClassitem(filter, 23);
		filter.setPay_classitem_id(itemThisMonth.getPay_classitem_id());
		payInput = dao.queryPayInput(filter).get(0);

		PayClassitem itemNextMonth = queryPayClassitem(filter, 24);
		filter.setPay_classitem_id(itemNextMonth.getPay_classitem_id());
		String year = filter.getPeriod().substring(0, 4);
		String month = filter.getPeriod().substring(4);
		filter.setPeriod(CalDate.getNextMonth(year, month));
		PayInput inputNext = dao.queryPayInput(filter).get(0);
		
		payInput.setPay_classitem_id_next(inputNext.getPay_classitem_id());
		payInput.setPay_input_id_next(inputNext.getPay_input_id());
		payInput.setPay_money_next(inputNext.getPay_money());
		return payInput;
	}
	
	public void insertCsPayInput(PayInput payInput) throws SQLException{
		dao.insertCsPayInput(payInput);
	}
	
	public void updateCsPayInput(PayInput payInput) throws SQLException{
		dao.updateCsPayInput(payInput);
	}
	
	public void saveCsPayInput(PayInput payInput) throws SQLException{
		if(payInput.getPay_input_id() != null){
			updateCsPayInput(payInput);
		}else{
			//查找最低工资调整项在工资类别中所对应id号
			PayClassitem itemThisMonth = queryPayClassitem(payInput, 23);
			PayClassitem itemNextMonth = queryPayClassitem(payInput, 24);
			
			//设定输入数据所对应工资调整项目classitem_id
			payInput.setCreator_code(payInput.getModifier_code());
			payInput.setPay_classitem_id(itemThisMonth.getPay_classitem_id());
			payInput.setPay_classitem_id_next(itemNextMonth.getPay_classitem_id());
			
			//新添加工资调整项目及数据
			insertCsPayInput(payInput);
		}
	}
	
	private PayClassitem queryPayClassitem(PayInput payInput, Integer segmentOrder){
		PayClazzItemFilter filter = new PayClazzItemFilter();
		filter.setCompanyCode(payInput.getCompany_code());
		filter.setPayTypeId(payInput.getPay_class_id());
		
		filter.setSegmentOrder(segmentOrder);
		return clazzItemDao.queryPayItemByFilter(filter).get(0);
	}
}
